package www.openteam.com.adapter.PrdctnMngmnt;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.adapter.policy.SendRcvApprlAdp;
import www.openteam.com.bean.StockTaxMaterial;

/**
 * Created by Administrator on 2019-12-24.
 */

public class StockTaxAdp extends RecyclerView.Adapter<StockTaxAdp.ViewHodler> {

    Context context;
    List<StockTaxMaterial.DataBeanX.DataBean> mSortedList = new ArrayList<>();
    List<Boolean> isflag = new ArrayList<>();
    boolean ischeck;
    StockTaxAdpSelectkClick stockTaxSelectAdpClick;

    public interface StockTaxAdpSelectkClick{
        void selection(int postion);
    }

    public StockTaxAdp(Context context) {
        this.context = context;
    }

    public void setmSortedList(List<StockTaxMaterial.DataBeanX.DataBean> mSortedList) {
        this.mSortedList = mSortedList;
        resetFlag();
    }

    public void setStockTaxSelectAdpClick(StockTaxAdpSelectkClick stockTaxSelectAdpClick) {
        this.stockTaxSelectAdpClick = stockTaxSelectAdpClick;
    }

    private void resetFlag(){
        int size = mSortedList.size();
        for (int i = 0; i < size; i++) {
                isflag.add(i, false);
        }
    }
    @Override
    public ViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_stock_tax_child, parent, false);
        return new ViewHodler(view);
    }

    @Override
    public void onBindViewHolder(ViewHodler holder, final int position) {
        StockTaxMaterial.DataBeanX.DataBean dataBean = mSortedList.get(position);
        holder.stockTakChildRow.setText(position+1+"");
        holder.stockTakChildMtrlName.setText(dataBean.getName());
        holder.stockTakChildBatchNo.setText(dataBean.getBatchNo());
        holder.stockTakChildLname.setText(dataBean.getLocation());
        holder.stockTakChildSpecs.setText(dataBean.getSpecs());
        holder.stockTakChildCknumb.setText(dataBean.getCknumb()+"");
        if (isflag.get(position)) {
            holder.aCard.setCardBackgroundColor(context.getResources().getColor(R.color.actionbar_background));
        }
        holder.aCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (stockTaxSelectAdpClick != null) {
                    int size = isflag.size();
                    for (int i = 0; i < size; i++) {
                        isflag.set(i, false);
                    }
                    isflag.set(position, true);
                    notifyDataSetChanged();
                    stockTaxSelectAdpClick.selection(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mSortedList.size();
    }

    public void removeItem(int position) {
        mSortedList.remove(mSortedList.get(position));
        resetFlag();
        notifyDataSetChanged();
    }

    public void addItem(StockTaxMaterial.DataBeanX.DataBean dataBean) {
        mSortedList.add(dataBean);
        resetFlag();
        notifyDataSetChanged();

    }

    public void setItem(int postion ,StockTaxMaterial.DataBeanX.DataBean dataBean) {
        mSortedList.set(postion,dataBean);
        notifyDataSetChanged();

    }

    public void clear() {
        mSortedList.clear();
        notifyDataSetChanged();

    }

    public static class ViewHodler extends RecyclerView.ViewHolder {
        @Bind(R.id.stock_tak_child_row)
        TextView stockTakChildRow;
        @Bind(R.id.stock_tak_child_mtrlName)
        TextView stockTakChildMtrlName;
        @Bind(R.id.stock_tak_child_BatchNo)
        TextView stockTakChildBatchNo;
        @Bind(R.id.stock_tak_child_lname)
        TextView stockTakChildLname;
        @Bind(R.id.stock_tak_child_specs)
        TextView stockTakChildSpecs;
        @Bind(R.id.stock_tak_child_cknumb)
        TextView stockTakChildCknumb;
        @Bind(R.id.lineitem)
        LinearLayout lineitem;
        @Bind(R.id.a_card)
        CardView aCard;

        public ViewHodler(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
